package cn.aiweiyi.qingjing.coupon.settlement.executor.impl;

import cn.aiweiyi.qingjing.coupon.comm.constant.CouponCategory;
import cn.aiweiyi.qingjing.coupon.comm.vo.CouponTemplateSDK;
import cn.aiweiyi.qingjing.coupon.comm.vo.SettlementInfo;

import cn.aiweiyi.qingjing.coupon.settlement.executor.AbstractExecutor;
import cn.aiweiyi.qingjing.coupon.settlement.executor.RuleExecutor;
import cn.aiweiyi.qingjing.coupon.settlement.util.BigDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 立减规则执行器
 */
@Slf4j
@Component
public class LiJianExecutor extends AbstractExecutor implements RuleExecutor {
    /**
     * 规则类型标记
     *
     * @return
     */
    @Override
    public CouponCategory ruleConfig() {
        return CouponCategory.LIJIAN;
    }

    /**
     * 优惠券规则的计算
     *
     * @param settlement 包含了选择的优惠券
     * @return 修正过的结算信息
     */
    @Override
    public SettlementInfo computeRule(SettlementInfo settlement) {
        // 计算商品原价，四舍五入
        double goodsSum = retain2Decimals(goodsCostSum(settlement.getGoodsInfos()));
        SettlementInfo probability = processGoodsTypeNotSatisfy(settlement, goodsSum);
        // 立减优惠券直接使用，没有门槛
        CouponTemplateSDK templateVO = settlement.getCouponAndTemplateInfos().get(0).getTemplate();
        // 得到模板规则中的立减金额
        double quota = (double) templateVO.getRule().getDiscount().getQuota();
        // 计算使用优惠券之后的价格 - 结算
        double costAfterDiscount = BigDecimalUtils.subtract(goodsSum, quota);
        settlement.setCost(costAfterDiscount > minCost() ? costAfterDiscount : minCost());
        log.debug("使用立减优惠券用户的应付金额从{}减少到{}元。", goodsSum, settlement.getCost());
        return settlement;
    }
}
